package com.spark.tags;

import java.util.Map;

import com.spark.core.SparkContext;
import com.spark.core.XMap;
import com.spark.util.ReferenceType;

public class DateField extends ViewComponent {

	private static final String DEFAULT_TEMPLATES = "dateField.ftl";
	private static final String DEFAULT_CLASSNAME = "s_df";
	
	
	public static final String PROPERTITY_RENDERER="renderer";
	
	protected int getDefaultWidth(){
		return 180;
	}
	
	protected int getDefaultHeight(){
		return 158;
	}
	
	protected String getDefaultClass(XMap view,SparkContext context){
		String className = super.getDefaultClass(view, context);
		StringBuilder sb = new StringBuilder();
		sb.append(className);
		if(!"".equals(className.trim()))sb.append(" ");
		sb.append(DEFAULT_CLASSNAME);
		return sb.toString();
	}
	
	public String getTemplate(){
		return DEFAULT_TEMPLATES;
	}
	
	public void init(XMap view, SparkContext context) throws Exception{
		super.init(view, context);
		String cn = (String) context.get(PROPERTITY_CLASSNAME);
		if(cn!=null) context.addJsonConfig(PROPERTITY_CLASSNAME, cn);
		String renderer = view.getString(PROPERTITY_RENDERER, "");
		if(!"".equals(renderer))
		context.addJsonConfig(PROPERTITY_RENDERER, new ReferenceType(renderer));
	}
	
	@SuppressWarnings("unchecked")
	protected String build(SparkContext context){
		Map map = context.getMap();
		StringBuilder sb = new StringBuilder();
		sb.append("<div id=\"").append(get(map,"id")).append("\" style=\"width:").append(get(map,"width")).append("px;").append(get(map,"style")).append("\"></div>\r");
		sb.append("<script>\r");
		sb.append("    new Spark.DateField(").append(get(map,"config")).append(");\r");
		sb.append("</script>\r");
		return sb.toString();
	}
}
